package com.chuanyang.driverh.mainhome.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.mainhome.OrderDetailRceiveActivity;
import com.chuanyang.driverh.mainhome.WaybilDetailActivity;
import com.chuanyang.driverh.mainhome.WaybilDetailDirverActivity;
import com.chuanyang.driverh.response2.MoneyFollowDriverEntity;
import com.chuanyang.driverh.response2.ResultMoneyFollow;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;

import java.util.List;


public class MoneyFollowDriverAdapter extends BaseListAdapter<MoneyFollowDriverEntity> {//首页竞价大厅的adapter
    private Activity activity;

    public MoneyFollowDriverAdapter(Activity activity) {
        super(activity);//此处不传入集合，则默认集合长度为0，在setdataresult（）方法中，添加数据并刷新
        this.activity = activity;
    }

    @Override
    public View getZkView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = getInflater().inflate(R.layout.home_item_money_follow_driver, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.line_left = convertView.findViewById(R.id.line_left);
            viewHolder.tv_has_money = (TextView) convertView.findViewById(R.id.tv_has_money);
            viewHolder.tv_order_no = (TextView) convertView.findViewById(R.id.tv_order_no);
            viewHolder.tv_order_time = (TextView) convertView.findViewById(R.id.tv_order_time);
            viewHolder.tv_city_start = (TextView) convertView.findViewById(R.id.tv_city_start);
            viewHolder.tv_city_end = (TextView) convertView.findViewById(R.id.tv_city_end);
            viewHolder.tv_goods_name = (TextView) convertView.findViewById(R.id.tv_goods_name);
//            viewHolder.tv_weight = (TextView) convertView.findViewById(R.id.tv_weight);
//            viewHolder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
//            viewHolder.tv_acture_weight = (TextView) convertView.findViewById(R.id.tv_acture_weight);
            viewHolder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
            viewHolder.tv_order_type = (TextView) convertView.findViewById(R.id.tv_order_type);
//            viewHolder.tv_see_assign= (TextView) convertView.findViewById(R.id.tv_see_assign);
            viewHolder.ll_div = (LinearLayout) convertView.findViewById(R.id.ll_div);
//            viewHolder.ll_see_assign= (LinearLayout) convertView.findViewById(R.id.ll_see_assign);


            viewHolder.tv_acture = (TextView) convertView.findViewById(R.id.tv_acture);
            viewHolder.tv_guapai = (TextView) convertView.findViewById(R.id.tv_guapai);
            viewHolder.tv_waybil_no = (TextView) convertView.findViewById(R.id.tv_waybil_no);
            viewHolder.ll_top = (LinearLayout) convertView.findViewById(R.id.ll_top);


            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();

        }
        final MoneyFollowDriverEntity data = getItem(position);
        if (data.getType() == 1) {
            viewHolder.ll_top.setVisibility(View.VISIBLE);
            viewHolder.ll_top.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(activity, OrderDetailRceiveActivity.class);
                    intent.putExtra("data", data.getOrderId() + "");
                    intent.putExtra("type", "0");
                    activity.startActivity(intent);
                }
            });


        } else {
            viewHolder.ll_top.setVisibility(View.GONE);
        }

        if (data.getDefrayStatus() == 0) {// 	0 未收款 2 已收款
            viewHolder.line_left.setBackgroundResource(R.color.text_assign_car_gray);
            viewHolder.tv_has_money.setTextColor(activity.getResources().getColor(R.color.text_assign_car_gray));
            viewHolder.tv_has_money.setText("未收款");
        } else {
            viewHolder.line_left.setBackgroundResource(R.color.text_get_order_orange);
            viewHolder.tv_has_money.setTextColor(activity.getResources().getColor(R.color.text_get_order_orange));
            viewHolder.tv_has_money.setText("已收款");
        }
        viewHolder.tv_order_no.setText(data.getOrderNo());
        viewHolder.tv_order_time.setText(data.getOrderTime());
        viewHolder.tv_city_start.setText(StringUtil.getSimpleMunicipality(data.getBeginCity()));
        viewHolder.tv_city_end.setText(StringUtil.getSimpleMunicipality(data.getEndCity()));
        viewHolder.tv_goods_name.setText(data.getGoodsNames());
        viewHolder.tv_acture.setText(StringUtil.getDouble(data.getSumGoodsWeight() + "") + "t/" + data.getSumGoodsNum() + "件");

        viewHolder.tv_guapai.setText(StringUtil.getDouble(data.getOriginalWeight() + "") + "t/" + data.getOriginalNum() + "件");

        viewHolder.tv_guapai.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

//        setGoodName(viewHolder.tv_goods_name, data.getGoodsNames());//货品名称

        if (data.getCurrency() == 1) {
            viewHolder.tv_money.setText(StringUtil.getTwoNum(data.getSumPrice() + "") + "¥");
        } else {
            viewHolder.tv_money.setText(StringUtil.getTwoNum(data.getSumPrice() + "") + "$");
        }

        viewHolder.tv_waybil_no.setText(data.getWaybillNo());


        if (data.getFleetId() != 0) {
            if (data.getOrderType() == 1) {//1竞价
                viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_bid_t);
            } else if (data.getOrderType() == 2) {//匹配
                viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_match_t);
            } else {//专线
                viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_special_t);
            }
        } else {
            if (data.getOrderType() == 1) {//1竞价
                viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_bid_p);
            } else if (data.getOrderType() == 2) {//匹配
                viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_match_p);
            } else {//专线
                viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_special_p);
            }
        }
        viewHolder.ll_div.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!StringUtil.isBlank(SharedPrefUtil.getFleetId())){
                    Intent intent=new Intent(activity, OrderDetailRceiveActivity.class);
                    intent.putExtra("data",data.getOrderId()+"");
                    intent.putExtra("type","0");
                    activity.startActivity(intent);

                }else {
                    if (data.getFleetId()!=0){
                        Intent intent=new Intent(activity, WaybilDetailDirverActivity.class);
                        intent.putExtra("waybilId",data.getWaybillId()+"");
                        intent.putExtra("type","0");
                        activity.startActivity(intent);
                    }else {
                        Intent intent=new Intent(activity, WaybilDetailActivity.class);
                        intent.putExtra("waybilId",data.getWaybillId()+"");
                        intent.putExtra("type","0");
                        activity.startActivity(intent);
                    }
                }
            }
        });


        return convertView;
    }


    private void setGoodName(TextView view, List<ResultMoneyFollow.OutData.MiddleData.InnderData.WaybillsBean> waybils) {
        StringBuilder builder = new StringBuilder();
        if (waybils == null) {
            view.setText("");
            return;
        }
        for (ResultMoneyFollow.OutData.MiddleData.InnderData.WaybillsBean info : waybils) {
            if (info != null)
                builder.append(info.getGoodsName() + ",");
        }
        String temp = builder.toString();
        if (temp.length() > 0) {
            String result = temp.substring(0, temp.length() - 1);
            view.setText(result);
        }
    }

    class ViewHolder {
        View line_left;
        TextView tv_has_money;
        TextView tv_order_no;
        TextView tv_order_time;
        TextView tv_city_start;
        TextView tv_city_end;
        TextView tv_goods_name;
        //        TextView tv_weight;
//        TextView tv_price;
//        TextView tv_acture_weight;
        TextView tv_money;
        TextView tv_order_type;
        //        TextView tv_see_assign;
        LinearLayout ll_div;
//        LinearLayout ll_see_assign;


        TextView tv_acture;
        TextView tv_guapai;
        TextView tv_waybil_no;
        LinearLayout ll_top;
    }
}
